package com.yh.correct.controller;

import java.io.Console;
import java.util.*;

import com.yh.common.utils.DateUtils;
import org.apache.commons.collections.ArrayStack;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.service.impl.EchartsServiceImpl;

/**
 * 作者:夏玉浩
 * 创建时间:2018-08-13
 * 描述:矫正人员报表控制层
 */
@RestController
@RequestMapping(value="/echarts")
public class EchartsController {
	/**
	 * logger4j日志
	 */
	private static Logger logger=Logger.getLogger(EchartsController.class);
	/**
	 * 自动注入
	 */
	@Autowired
	private EchartsServiceImpl echartsServiceImpl;
	
	@RequestMapping("/showQuarterlyReport")
	public ModelAndView showQuarterlyReport(){
		return new ModelAndView("statisticalReports/quarterlyReports");
	}
	/**
	 *季度报表 
	 */
	@RequestMapping(value="/viewEcharts")
	public Map<String,Object> quarterlyReports(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("category", echartsServiceImpl.findMonth());
		map.put("pilot", echartsServiceImpl.findPilotCount()); 
		map.put("socialWorkers", echartsServiceImpl.findSocialWorkers());
		map.put("governmentWorkers", echartsServiceImpl.findGovernmentWorkers() );
		map.put("successfulCorrection", echartsServiceImpl.findSuccessfulCorrection());
		map.put("name", echartsServiceImpl.findName());
		return map;
	}
	/**
	 *季度报表真实数据版
	 */
	@RequestMapping(value="/quarterView")
	public Map<String,Object> quarterReports(){
		Map<String,Object> map = new HashMap<String,Object>();
		//设置类别
		List<String> category=new ArrayList<String>();
		category.add("第一季度");
		category.add("第二季度");
		category.add("第三季度");
		category.add("第四季度");
		map.put("category", category);
		//对本年每个季度新入矫人员数量进行处理
		List<Date> newCorrect=echartsServiceImpl.getAllStartDateByYear(String.valueOf((Integer.parseInt(DateUtils.getYear())-1)));
		List<Integer> newCorrectCount=quarterCount(dealDateByQuarter(newCorrect));
		map.put("newCorrectCount", newCorrectCount);
		//对本年每个季度出矫人员数量进行处理
		List<Date> oldCorrect=echartsServiceImpl.getAllEndDateByYear(String.valueOf((Integer.parseInt(DateUtils.getYear())-1)));
		List<Integer> oldCorrectCount=quarterCount(dealDateByQuarter(oldCorrect));
		map.put("oldCorrectCount", oldCorrectCount);
		//对本年每个季度目前在矫人员数量进行处理
		List<Integer> correctedCount=correctedCount(newCorrectCount,oldCorrectCount);
		map.put("correctedCount", correctedCount);

		//获取新入矫人员犯罪类型
		List<String> crimes=echartsServiceImpl.getCrimeByYear(String.valueOf((Integer.parseInt(DateUtils.getYear())-1)));
		//下面四个集合是根据季度对犯罪类型进行分组
		List<String> crimes1=new ArrayList<String>();
		List<String> crimes2=new ArrayList<String>();
		List<String> crimes3=new ArrayList<String>();
		List<String> crimes4=new ArrayList<String>();
		for(int i=0;i<newCorrect.size();i++){
			String month=DateUtils.getMonth(newCorrect.get(i));
			if(month.equals("12") || month.equals("01") || month.equals("02")){
				crimes1.add(crimes.get(i));
			}else if(month.equals("03") || month.equals("04") || month.equals("05")){
				crimes2.add(crimes.get(i));
			}else if(month.equals("06") || month.equals("07") || month.equals("08")){
				crimes3.add(crimes.get(i));
			}else{
				crimes4.add(crimes.get(i));
			}
		}

		//下面四个是每个季度的犯罪类型列表
		List<Integer> crimesCount1=quarterCrimeCount(crimes1);
		List<Integer> crimesCount2=quarterCrimeCount(crimes2);
		List<Integer> crimesCount3=quarterCrimeCount(crimes3);
		List<Integer> crimesCount4=quarterCrimeCount(crimes4);
		//每个犯罪类型的数量
		List<Integer> robberyCount=new ArrayList<Integer>();
		List<Integer> theftCount=new ArrayList<Integer>();
		List<Integer> killCount=new ArrayList<Integer>();
		List<Integer> securityCount=new ArrayList<Integer>();
		List<Integer> trafficCount=new ArrayList<Integer>();
		List<Integer> otherCrimeCount=new ArrayList<Integer>();

		//数据处理
		robberyCount.add(crimesCount1.get(0));
		robberyCount.add(crimesCount2.get(0));
		robberyCount.add(crimesCount3.get(0));
		robberyCount.add(crimesCount4.get(0));

		theftCount.add(crimesCount1.get(1));
		theftCount.add(crimesCount2.get(1));
		theftCount.add(crimesCount3.get(1));
		theftCount.add(crimesCount4.get(1));

		killCount.add(crimesCount1.get(2));
		killCount.add(crimesCount2.get(2));
		killCount.add(crimesCount3.get(2));
		killCount.add(crimesCount4.get(2));

		securityCount.add(crimesCount1.get(3));
		securityCount.add(crimesCount2.get(3));
		securityCount.add(crimesCount3.get(3));
		securityCount.add(crimesCount4.get(3));

		trafficCount.add(crimesCount1.get(4));
		trafficCount.add(crimesCount2.get(4));
		trafficCount.add(crimesCount3.get(4));
		trafficCount.add(crimesCount4.get(4));

		otherCrimeCount.add(crimesCount1.get(5));
		otherCrimeCount.add(crimesCount2.get(5));
		otherCrimeCount.add(crimesCount3.get(5));
		otherCrimeCount.add(crimesCount4.get(5));

		map.put("robberyCount",robberyCount);
		map.put("theftCount",theftCount);
		map.put("killCount",killCount);
		map.put("securityCount",securityCount);
		map.put("trafficCount",trafficCount);
		map.put("otherCrimeCount",otherCrimeCount);
		//设置图例
		List<String> quarterName=new ArrayList<String>();
		quarterName.add("新入矫人数");
		quarterName.add("入矫结束人数");
		quarterName.add("目前在矫人数");
		quarterName.add("抢劫罪");
		quarterName.add("故意杀人罪");
		quarterName.add("盗窃罪");
		quarterName.add("重大交通事故罪");
		quarterName.add("危害国家安全罪");
		quarterName.add("其它");
		map.put("quarterName",quarterName);

		return map;
	}


	/**
	 * 对当前在矫人员数量进行统计
	 * @param newCorrectCount 已经按照季度统计过的新入在矫人员集合
	 * @param oldCorrectCount 已经按照季度统计过的出矫人员集合
	 * @return
	 */
	public List<Integer> correctedCount(List<Integer> newCorrectCount,List<Integer> oldCorrectCount){
		List<Integer> correctedCount=new ArrayList<Integer>();
		List<Date> corrected=echartsServiceImpl.getCorrectedCountByYear(String.valueOf((Integer.parseInt(DateUtils.getYear())-1)));
		correctedCount.add(corrected.size()+newCorrectCount.get(0)-oldCorrectCount.get(0));
		correctedCount.add(corrected.size()+newCorrectCount.get(1)-oldCorrectCount.get(1));
		correctedCount.add(corrected.size()+newCorrectCount.get(2)-oldCorrectCount.get(2));
		correctedCount.add(corrected.size()+newCorrectCount.get(3)-oldCorrectCount.get(3));
		return correctedCount;
	}
	/**
	 * 根据传进来的月份集合，来进行季度统计
	 * @param list
	 * @return
	 */
	public List<Integer> quarterCount(List<String> list){
		List<Integer> count=new ArrayList<Integer>();
		int quarter1=0;
		int quarter2=0;
		int quarter3=0;
		int quarter4=0;
		for(String l:list){
			if(l.equals("12") || l.equals("01") || l.equals("02")){
				quarter1++;
			}else if(l.equals("03") || l.equals("04") || l.equals("05")){
				quarter2++;
			}else if(l.equals("06") || l.equals("07") || l.equals("08")){
				quarter3++;
			}else{
				quarter4++;
			}
		}
		count.add(quarter1);
		count.add(quarter2);
		count.add(quarter3);
		count.add(quarter4);
		return count;
	}

	/**
	 * 根据季度对时间集合进行处理
	 * @param list
	 * @return
	 */
	public List<String> dealDateByQuarter(List<Date> list){
		List<String> month=new ArrayList<String>();
		for(Date l:list){
			if(l!=null){
				month.add(DateUtils.getMonth(l));
			}
		}
		return month;
	}

	/**
	 * 根据传来的数据对犯罪类型进行统计并存放
	 * @param crimeName
	 * @return
	 */
	public List<Integer> quarterCrimeCount(List<String> crimeName){
		List<Integer> quarterCrimeCount=new ArrayList<Integer>();
		int robbery=0;//抢劫罪数量
		int traffic=0;//重大交通事故罪数量
		int security=0;//危害国家安全罪数量
		int theft=0;//盗窃罪
		int kill=0;//故意杀人罪
		int otherCrime=0;//其他犯罪类型

		for(String c:crimeName){
			if(null!=c && !"".equals(c)){
				if(c.equals("抢劫罪")){
					robbery++;
				}else if(c.equals("盗窃罪")){
					theft++;
				}else if(c.equals("故意杀人罪")){
					kill++;
				}else if(c.equals("危害国家安全罪")){
					security++;
				}else if(c.equals("重大交通事故罪")){
					traffic++;
				}else{
					otherCrime++;
				}
			}
		}
		//数据存放
		quarterCrimeCount.add(robbery);
		quarterCrimeCount.add(theft);
		quarterCrimeCount.add(kill);
		quarterCrimeCount.add(security);
		quarterCrimeCount.add(traffic);
		quarterCrimeCount.add(otherCrime);
		return quarterCrimeCount;
	}



	/**
	 * 进入月度首页 
	 */
	@RequestMapping("/showMonthlyReport")
	public ModelAndView showMonthlyReport(){
		return new ModelAndView("statisticalReports/monthlyReports");
	}
	
	/**
	 * 月度报表
	 */
	@RequestMapping(value="/view")
	public Map<String,Object> monthlyReport(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", echartsServiceImpl.findProperties() );
		map.put("value", echartsServiceImpl.findAllCount());
		return map;
	}

	/**
	 * 月度报表(真实数据版)
	 */
	@RequestMapping(value="/monthView")
	public Map<String,Object> monthReport(){
 		Map<String,Object> map = new HashMap<String,Object>();
		List<String> name=new ArrayList<String>(); //图表数据项名
		List<Integer> value=new ArrayList<Integer>(); //图表数据项数据

		List<Integer> age=echartsServiceImpl.getAllAge();//年龄
		List<String> sex=echartsServiceImpl.getAllSex();//性别
		List<String> culture=echartsServiceImpl.getAllCulture();//文化程度
		List<String> crime=echartsServiceImpl.getAllCrime();//犯罪类型
		List<String> household=echartsServiceImpl.getAllHousehold();//户籍类型

		//对年龄进行处理
		int before18=0;//18岁以前的数量
		int from18to45=0;//18岁到45岁数量
		int after45=0;//45岁之后数量
		for(Integer a:age){
			if(null!=a && !"".equals(a)){
				if(a<18){
					before18++;
				}else if(a>=18 && a<=45){
					from18to45++;
				}else{
					after45++;
				}
			}

		}


		//对性别进行处理
		int man=0;//男性数量
		int men=0;//女性数量
		for(String s:sex){
			if(null!=s && !"".equals(s)){
				if(s.equals("男")){
					man++;
				}else{
					men++;
				}
			}
		}

		//对文化程度进行处理
		int primary=0;//小学学历数量
		int junior=0;//初中学历数量
		int high=0;//高中学历数量
		int collegeOrBachelor=0;//专科或者本科
		int otherCulture=0;//其他文化程度
		for(String c:culture){
			if(null!=c && !"".equals(c)){
				if(c.equals("小学")){
					primary++;
				}else if(c.equals("初中")){
					junior++;
				}else if(c.equals("高中")){
					high++;
				}else if(c.equals("本科") || c.equals("专科")){
					collegeOrBachelor++;
				}else{
					otherCulture++;
				}
			}
		}

		//对犯罪类型进行处理
		int robbery=0;//抢劫罪数量
		int traffic=0;//重大交通事故罪数量
		int security=0;//危害国家安全罪数量
		int theft=0;//盗窃罪
		int kill=0;//故意杀人罪
		int social=0;//扰乱社会秩序罪
		int corruption=0;//贪污贿赂罪
		int otherCrime=0;//其他犯罪类型

		for(String c:crime){
			if(null!=c && !"".equals(c)){
				if(c.equals("抢劫罪")){
					robbery++;
				}else if(c.equals("重大交通事故罪")){
					traffic++;
				}else if(c.equals("危害国家安全罪")){
					security++;
				}else if(c.equals("盗窃罪")){
					theft++;
				}else if(c.equals("故意杀人罪")){
					kill++;
				}else if(c.equals("扰乱社会秩序罪")){
					social++;
				}else if(c.equals("贪污贿赂罪")){
					corruption++;
				}else{
					otherCrime++;
				}
			}
		}

		//对户籍类型进行处理
		int city=0;//城市户口数量
		int village=0;//农村户口数量
		int otherHousehold=0;//其他户籍类型，比如黑(无)户口
		for(String h:household){
			if(null!=h && !"".equals(h)){
				if(h.equals("城市户口")){
					city++;
				}else if(h.equals("农村户口")){
					village++;
				}else{
					otherHousehold++;
				}
			}
		}

		//对处理过的数据进行添加。
		name.add("犯罪类型");
		name.add("男女比例");
		name.add("年龄情况");
		name.add("文化程度");
		name.add("户籍类型");
		//犯罪类型分类
		name.add("抢劫罪");
		name.add("重大交通事故罪");
		name.add("危害国家安全罪");
		name.add("盗窃罪");
		name.add("故意杀人罪");
		name.add("扰乱社会秩序罪");
		name.add("贪污贿赂罪");
		name.add("其它犯罪");
		//添加值
		value.add(robbery);
		value.add(traffic);
		value.add(security);
		value.add(theft);
		value.add(kill);
		value.add(social);
		value.add(corruption);
		value.add(otherCrime);
		//男女分类
		name.add("男");
		name.add("女");
		value.add(man);
		value.add(men);
		//年龄分类
		name.add("未满18岁");
		name.add("18至45岁");
		name.add("45岁以上");
		value.add(before18);
		value.add(from18to45);
		value.add(after45);

		//文化程度分类
		name.add("小学");
		name.add("初中");
		name.add("高中");
		name.add("本科或者专科");
		name.add("其它学历");
		value.add(primary);
		value.add(junior);
		value.add(high);
		value.add(collegeOrBachelor);
		value.add(otherCulture);
		//户籍类型分类
		name.add("城市户口");
		name.add("农村户口");
		name.add("其它");
		value.add(city);
		value.add(village);
		value.add(otherHousehold);

		map.put("name", name);
		map.put("value", value);
		return map;
	}
}
